﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Windows.Navigation;
using System.IO.IsolatedStorage;
using System.Windows.Controls.Primitives;
using System.Windows.Threading;
using Microsoft.Phone.Net.NetworkInformation;
using System.IO;
using System.Windows.Media.Imaging;
using Microsoft.Xna.Framework.Media;
using System.Windows.Resources;


namespace KOF.Views
{
    public partial class News : PhoneApplicationPage
    {

        Stack<Uri> history = new Stack<Uri>();
        Stack<Uri> historynext = new Stack<Uri>();

        string shoeid1;
        Popup newspopupScreen = new Popup();
        List<Uri> HistoryStack;
        int HistoryStack_Index;
        bool fromHistory;
        public News()
        {
            try
            { //This Code checks  internet Connection
                if (NetworkInterface.NetworkInterfaceType != NetworkInterfaceType.None)
                {

                }
                else
                {
                    MessageBox.Show("Please check your internet connection!");
                }
                InitializeComponent();

                HistoryStack = new List<Uri>();
                HistoryStack_Index = 0;
                fromHistory = false;


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {


            try
            {

                GoogleAnalytics.EasyTracker.GetTracker().SendView("News tab");
                // Gets Url from Query String. if it finds will set in browser control otherwise it will set  http://www.kicksonfire.com/ Default.
                string url = "";
                if (NavigationContext.QueryString.TryGetValue("url", out url))
                {
                    IsolatedStorageSettings.ApplicationSettings["urlkof"] = NavigationContext.QueryString["url"];
                    ctlBrowser.Source = new Uri(NavigationContext.QueryString["url"], UriKind.Absolute);
                }
                else
                {
                    IsolatedStorageSettings.ApplicationSettings["urlkof"] = "http://www.kicksonfire.com/";
                    ctlBrowser.Source = new Uri("http://www.kicksonfire.com/", UriKind.Absolute);
                }


                string shoeid = "";
                if (NavigationContext.QueryString.TryGetValue("id", out shoeid))
                {

                    shoeid1 = shoeid;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            base.OnNavigatedTo(e);
        }

        //Redirect To Setting Screen
        private void btnSettings_Click(object sender, EventArgs e)
        {
            try
            {
                if (newspopupScreen.IsOpen == true)
                {
                    newspopupScreen.IsOpen = false;
                }


                this.NavigationService.Navigate(new Uri("/Views/Settings.xaml", UriKind.Relative));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        //Redirect To Release Screen
        private void btnReleases_Click(object sender, EventArgs e)
        {

            if (newspopupScreen.IsOpen == true)
            {
                newspopupScreen.IsOpen = false;
            }
            try
            {
                IsolatedStorageSettings.ApplicationSettings["loader"] = false;
                this.NavigationService.Navigate(new Uri("/Views/MainPage.xaml", UriKind.Relative));

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        //Redirect To Favorites Screen
        private void btnFavorites_Click(object sender, EventArgs e)
        {
            try
            {
                if (newspopupScreen.IsOpen == true)
                {
                    newspopupScreen.IsOpen = false;
                }
                this.NavigationService.Navigate(new Uri("/Views/Favorites.xaml", UriKind.Relative));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        //Sets first Url of Browser Control
        private void btnHome_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                if (IsolatedStorageSettings.ApplicationSettings.Contains("urlkof"))
                {

                    ctlBrowser.Source = new Uri(IsolatedStorageSettings.ApplicationSettings["urlkof"].ToString(), UriKind.Absolute);

                }
                else
                {
                    ctlBrowser.Source = new Uri("http://www.kicksonfire.com/", UriKind.Absolute);


                }
            }
            catch (Exception ex)
            {
                //  ctlBrowser.Source = new Uri("http://www.kicksonfire.com/", UriKind.Absolute);
                MessageBox.Show(ex.Message.ToString());

            }
        }

        // Share popup Will Open
        private void btnShare_MouseEnter(object sender, MouseEventArgs e)
        {

            try
            {
                String titleurl = (string)ctlBrowser.InvokeScript("eval", "document.title");
                String descurl = (string)ctlBrowser.InvokeScript("eval", "document.metaDesc");
                String imgurl = (string)ctlBrowser.InvokeScript("eval", "document.imgSRC");
                // MessageBox.Show(descurl);

                string url = "";
                if (NavigationContext.QueryString.TryGetValue("url", out url))
                {

                    newspopupScreen.Child =
                        new PopupUserControl(shoeid1, titleurl, descurl, imgurl, url);
                    newspopupScreen.IsOpen = true;
                    newspopupScreen.VerticalOffset = 150;
                    newspopupScreen.HorizontalOffset = 80;
                    newspopupScreen.Closed += (s1, e1) =>
                    {

                    };

                }
                else
                {
                    url = "http://www.kicksonfire.com/";
                    newspopupScreen.Child =
                     new PopupUserControl(shoeid1, titleurl, descurl, imgurl, url);
                    newspopupScreen.IsOpen = true;
                    newspopupScreen.VerticalOffset = 150;
                    newspopupScreen.HorizontalOffset = 80;
                    newspopupScreen.Closed += (s1, e1) =>
                    {
                        // Add you code here to do something
                        // when the Popup is closed
                    };
                }
            }
            catch (Exception ex)
            {
                ctlBrowser.Source = new Uri("http://www.kicksonfire.com/", UriKind.Absolute);
            }

        }


        // Close Popup When we Press Back Key
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                //Check if the PopUp window is open
                if (newspopupScreen.IsOpen)
                {
                    //Close the PopUp Window
                    newspopupScreen.IsOpen = false;
                    newspopupScreen.Visibility = System.Windows.Visibility.Collapsed;

                    //Keep the back button from
                    //navigating away from the current page
                    e.Cancel = true;
                }
                else
                {
                    //There is no PopUp open, use the back button normally
                    base.OnBackKeyPress(e);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }


        // Set History in Browser when browser is loaded
        private void ctlBrowser_Navigated(object sender, NavigationEventArgs e)
        {
            try
            {
                if (!fromHistory)
                {
                    if (HistoryStack_Index < HistoryStack.Count)
                    {
                        HistoryStack.RemoveRange(HistoryStack_Index, HistoryStack.Count - HistoryStack_Index);
                    }
                    HistoryStack.Add(e.Uri);
                    HistoryStack_Index += 1;

                }
                fromHistory = false;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

        }

        // BackWard History
        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (HistoryStack_Index > 1)
                {
                    HistoryStack_Index -= 1;
                    fromHistory = true;
                    ctlBrowser.Navigate(HistoryStack[HistoryStack_Index - 1]);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

        }

        // Forward History
        private void btnforward_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (HistoryStack_Index < HistoryStack.Count)
                {
                    HistoryStack_Index += 1;
                    fromHistory = true;
                    ctlBrowser.Navigate(HistoryStack[HistoryStack_Index - 1]);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }



    }
}